from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core.validators import MinValueValidator, MaxValueValidator
from django.core.exceptions import ValidationError
from django.db.models import Count


# Create your models here.
class myUser(AbstractUser):
    nickName = models.CharField(max_length=100, verbose_name="昵称", default="", blank=True)
    userImg = models.ImageField(upload_to='userImg/', verbose_name="用户头像", default='default.jpg')
    isVip = models.BooleanField(default=False, verbose_name="是否为会员")
    sex = models.CharField(
        max_length=1,
        verbose_name="性别",
        default='M',
        choices=[('M', '男'), ('F', '女')]
    )
    groups = models.ManyToManyField(
        'auth.Group',
        related_name='myuser_set',
        blank=True,
        help_text='The groups this user belongs to.',
        verbose_name='groups',
    )
    user_permissions = models.ManyToManyField(
        'auth.Permission',
        related_name='myuser_set',
        blank=True,
        help_text='Specific permissions for this user.',
        verbose_name='user permissions',
    )

    class Meta:
        permissions = (
            ('custom_view_myuser', 'Can view myUser'),
        )


class VideoTags:
    TIME_CHOICES = [
        ('ALL', '全部日期'),
        ('1D', '最近一天'),
        ('1W', '最近一周'),
        ('1M', '最近一月'),
        ('3M', '最近三月'),
        ('6M', '最近半年'),
        ('1Y', '最近一年'),
    ]

    TYPE_CHOICES = [
        ('ALL', '全部类型'),
        ('COMEDY', '喜剧'),
        ('ACTION', '动作'),
        ('ROMANCE', '爱情'),
        ('SCIFI', '科幻'),
        ('ANIMATION', '动画'),
        ('MYSTERY', '悬疑'),
        ('HORROR', '恐怖'),
        ('CRIME', '犯罪'),
        ('DOCUMENTARY', '纪录片'),
        ('SHORT', '短片'),
        ('OTHER', '其他'),
    ]

    LENGTH_CHOICES = [
        ('ALL', '全部时长'),
        ('S60', '60分钟以下'),
        ('M60_90', '60-90分钟'),
        ('M90_120', '90-120分钟'),
        ('L120', '120分钟以上'),
    ]
    HOT_CHOICES = [
        ('ALL', '全部类型'),
        ('HOT', '热门'),
        ('NEW', '最新'),
        ('RECOMMEND', '推荐'),
        ('NORMAL', '普通')
    ]
    CLASS_CHOICES = [
        ('ALL', '全部类型'),
        ('FREE', '免费'),
        ('VIP', '会员'),
        ('PREMIUM', '付费')
    ]


class video(models.Model):
    title = models.CharField(max_length=100, verbose_name="视频标题")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    time = models.DateField(verbose_name="上映时间")
    info = models.CharField(max_length=255, verbose_name="视频信息")
    actor = models.CharField(max_length=100, verbose_name="演员")
    director = models.CharField(max_length=100, verbose_name="导演")
    img = models.ImageField(upload_to='videoImg/', verbose_name="封面", default='default.jpg')
    hot_tag = models.CharField(
        max_length=20,
        choices=VideoTags.HOT_CHOICES,
        default='ALL',
        verbose_name="热度标签"
    )
    class_tag = models.CharField(
        max_length=20,
        choices=VideoTags.CLASS_CHOICES,
        default='ALL',
        verbose_name="分类标签"
    )

    time_tag = models.CharField(
        max_length=100,
        choices=VideoTags.TIME_CHOICES,
        default='ALL',
        verbose_name="时间标签"
    )
    type_tag = models.CharField(
        max_length=100,
        choices=VideoTags.TYPE_CHOICES,
        default='ALL',
        verbose_name="类型标签"
    )
    length_tag = models.CharField(
        max_length=100,
        choices=VideoTags.LENGTH_CHOICES,
        default='ALL',
        verbose_name="时长标签"
    )

    def __str__(self):
        return self.title


class comment(models.Model):
    user = models.ForeignKey(myUser, on_delete=models.CASCADE)
    video = models.ForeignKey(video, on_delete=models.CASCADE)
    content = models.CharField(max_length=255, verbose_name="评论内容")
    time = models.DateTimeField(auto_now_add=True, verbose_name="评论时间")

    def __str__(self):
        return f"Comment by {self.user} on {self.video}"


class like(models.Model):
    user = models.ForeignKey(myUser, on_delete=models.CASCADE)
    comment = models.ForeignKey(comment, related_name='like', on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    is_like = models.BooleanField(default=False, verbose_name="点赞")
    is_dislike = models.BooleanField(default=False, verbose_name="点踩")

    class Meta:
        unique_together = (('user', 'comment'),)

    def __str__(self):
        return f"{self.user.username} liked {self.comment}"


class score(models.Model):
    user = models.ForeignKey(myUser, on_delete=models.CASCADE)
    video = models.ForeignKey(video, on_delete=models.CASCADE)
    score = models.IntegerField(
        default=0,
        verbose_name="评分",
        validators=[
            MinValueValidator(0, message='评分不能小于0'),
            MaxValueValidator(10, message='评分不能大于10')
        ]
    )
    time = models.DateTimeField(auto_now_add=True, verbose_name="评分时间")

    class Meta:
        unique_together = (('user', 'video'),)

    def __str__(self):
        return f"{self.user.username} scored {self.score} for {self.video.title}"


class collect(models.Model):
    user = models.ForeignKey(myUser, on_delete=models.CASCADE)
    video = models.ForeignKey(video, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="收藏时间")
    is_collect = models.BooleanField(default=False, verbose_name="收藏")

    class Meta:
        unique_together = (('user', 'video'),)

    def __str__(self):
        return f"{self.user.username} collected {self.video.title}"
